﻿$("document").ready(function(){

	if($("#marcasCarousel .box-banner").length > 3){
		$("#marcasCarousel  .box-banner:lt(6)").css("display", "block");
		$("#marcasCarousel ").append('<a href="#" class="btn prev disabled" /><a href="#" class="btn next" />');
		$("#marcasCarousel  .box-banner").animate({"opacity": 1}, 200);
		$("#marcasCarousel  a.leftcontrol").click(function(){ brandSliderMovePrevious(); return false; });
		$("#marcasCarousel  a.rightcontrol").click(function(){ brandSliderMoveNext(); return false; });
	}


	$(".product-field").each(function(){
		$(this).find("li").each(function(){
			var autorName = $(this).text();
			$(this).html("<a href='/" + autorName + "'>" + autorName + "</a>");
		});
	});
	$(".category").each(function(){
		$(this).find("a").text("+ " + $(this).find("a").text());
	});

	if($("#selector #O").length > 0){ $("#selector #O option:eq(8), #selector #O option:eq(7)").remove(); }
	
	if($("body").is(".produto")){
	
		$(".dadosLivro .bread-crumb li:gt(1)").css("display", "inline");
	
		if($("#calculoFrete").length > 0){
		$(".shipping-value").live('click', function(){
			var intervalo = setInterval(function (){ 
				if($("#calculoFrete .content").length > 0){
					$("#calculoFrete .content").append("<a href='#' class='closeButton'></a>"); 
					clearInterval(intervalo);
				}
			}, 200);
		});
			$(".closeButton").live('click', function(){
				$("#calculoFrete").empty();
				return false;
			});
		}
		
		var autorName = $("div.dadosLivro #caracteristicas table td.value-field.Autor").text();
		$("div.dadosLivro #caracteristicas table td.value-field.Autor").html("<a href='/" + autorName + "'>" + autorName + "</a>");
		
		$(".nivo-controlNav").css("margin-left", "-" + $(".nivo-controlNav").width() / 2 + "px");
		
		
		setInterval(function(){
			if($(".ColecaoQVVTall").is(":empty"))
				$("#ColecaoQVVT").css("display", "none").next('hr').css("display", "none");
			
			if($(".ColecaoQVCTall").is(":empty"))
				$("#ColecaoQVCT").css("display", "none").next('hr').css("display", "none");
				
			if($(".ColecaoQCCTall").is(":empty"))
				$("#ColecaoQCCT").css("display", "none");
		}, 200);
		
		
		$(".vejaTambem a").each(function(){
			var target = $(this).attr("href");
			if(target == "#" || $(target).length == 0 || $(target).is(':empty')){
				$(this).parent().remove();
			}else{
				if($(target).find(".ColecaoQCCTall").length > 0 && $(target).find(".ColecaoQCCTall").is(":empty"))
					$(this).parent().remove();
			}
		});
		$(".vejaTambem a").live('click', function(){
			var target = $(this).attr("href");
			var position = $(target).offset();
			$('html, body').animate({scrollTop: (position.top - 20)}, 200);
			
			return false;
		});
	
	
	}
	if($("body").is(".home")){
	
		if($(".vitrine_home > ul > li, .promocoes > ul > li").length < 4){
			$(".vitrine_home > ul > li.last, .promocoes > ul > li.last").removeClass("last");
		}
		
		$(".vitrine_home li img, .promocoes li img").live("mouseover", function(){
			$(this).parents("li").find('#sinopse').css('display', 'block');
		});
		$(".vitrine_home li img, .promocoes li img").live("mouseout", function(){
			$(this).parents("li").find('#sinopse').css('display', 'none');
		});
	
		$(".nivoSlider").after('<div id="nivoLinks"></div>');
		$(".nivoSlider .box-banner").each(function(i){
			var url = $(this).find("a").attr("href");
			if(typeof(url) != "undefined"){
				$(this).find("a img").attr("title", "#link_" + i);
				$("#nivoLinks").append('<div id="link_' + i + '"><a href="' + url + '" ></a></div>');
			}
		});
		
		$(".nivoSlider").nivoSlider({
			effect: 'fade',
			slices: 17,
			boxCols: 8,
			boxRows: 4,
			animSpeed: 700,
			pauseTime: 5000,
			startSlide: 0,
			directionNav: true,
			directionNavHide: true,
			controlNav: true,
			keyboardNav: true,
			pauseOnHover: true,
			randomStart: false
		});
	
		
		
		$(".mais_vendidos li").each(function(){
			var productName = $(this).find(".productName").text();
			if(productName.length >= 40){
				var productNameReduced = productName.substr(0, 40);
				productNameReduced = productNameReduced.substr(0, productNameReduced.lastIndexOf(" ")) + " ...";
				$(this).find(".productName").text(productNameReduced);
			}
			$(this).find(".categoryName ul li").each(function(){
				var authorName = $(this).find("a").text();
				if(authorName.length > 15){
					var authorNameReduced = authorName.substr(0, 15);
					authorNameReduced = authorNameReduced.substr(0, authorNameReduced.lastIndexOf(" ")) + " ...";
					$(this).find("a").text(authorNameReduced);
				}
			});
		});
		
	}
});


function brandSliderMoveNext(){
	if(!$(".rightcontrol").hasClass("disabled") && $("#marcasCarousel .box-banner:animated").length == 0){
		$("#marcasCarousel .box-banner:visible:last").next().css({"display": "block", "opacity": "0"}).animate({"opacity": "1"}, 500);
		$("#marcasCarousel .box-banner:visible:first").animate({"opacity": 0, "margin-left":"-150px"}, 500, function(){
			$(this).css({"margin-left":"0", "display":"none"});
			if($("#marcasCarousel .box-banner:visible:last").next('.box-banner').length == 0)
				$("#marcasCarousel a.rightcontrol").addClass("disabled");
			if($("#marcasCarousel a.leftcontrol").hasClass("disabled"))
				$("#marcasCarousel a.leftcontrol").removeClass("disabled");
		});
	}
    if(){
    }
}

function brandSliderMovePrevious(){
	if(!$(".leftcontrol").hasClass("disabled") && $("#marcasCarousel .box-banner:animated").length == 0){
		$("#marcasCarousel .box-banner:visible:first").prev().css({"display": "block", "opacity": 0, "margin-left": "-150px"}).animate({"opacity": 1, "margin-left": "0"}, 500);
		$("#marcasCarousel .box-banner:visible:last").animate({"opacity": 0}, 500, function(){
			$(this).css({"margin-right":"0", "display":"none"});
			if($("#marcasCarousel .box-banner:visible:first").prev('.box-banner').length == 0)
				$("#marcasCarousel a.leftcontrol").addClass("disabled");
			if($("#marcasCarousel a.rightcontrol").hasClass("disabled"))
				$("#marcasCarousel a.rightcontrol").removeClass("disabled");
		});
	}
}


if(typeof(jQ)=="undefined") var jQ=$(document);
if(typeof(String.prototype.trim)!=="function"){ String.prototype.trim=function(){ return this.replace(/^\s+|\s+$/g,""); }; }

var site = {
	init: function(){
		site.menu();
	},
	ajaxStop: function(){
		
	},
	
	menu: function(){
		$('h5:first, h5:first + ul + h5 + ul + h5').addClass('linkOculto');
		
		var menuOutObject;
		var menuOutTimer;
		$(function() {
		    $('.departamento .navigator h4 + ul').each(function(){
			    if ($(this).find('li').length > 0) {
				    $(this).prev('h4').find('a').attr("href", "javascript:void(0)");
			    }
			});
			
			$('.departamento .navigator h4, .linkOculto').click(
				function() {
					menuOutObject = $(this).next();
					y = $(this).offset().left;
					menuOutObject.css('left', y);
				
					if (!menuOutObject.is(':visible')) {
						hideMenuSubItems($('.departamento .navigator h4 + ul:visible, .linkOculto + ul:visible'));
					}
					clearTimeout(menuOutTimer);
					menuOutObject.fadeIn();
				}
				
			);	
		});

		function hideMenuSubItems(o) {
			o.fadeOut(10);
		}
		
		$('h5:first').addClass('linkOculto');
	}
	
};

$(site.init);
$(document).ajaxStop(site.ajaxStop);